import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute, ParamMap } from '@angular/router';
import { SimpleAnimate } from '../../../share/animation/my.simple.animation';

import { Grid } from '../../../share/model/grid.model';

import { HttpService } from '../../../share/http/http.service';
import { NoticeService } from '../../../share/service/notice.service';

import { ConfirmDialogService } from '../../../share/service/confirm.service';

@Component({
  selector: 'hk-menu-list',
  templateUrl: './menu.list.component.html',
  styleUrls: ['./menu.list.component.css'],
  animations: [SimpleAnimate]
})
export class MenuListComponent implements OnInit {

  private columns = [
    { title: '', field: 'id', hidden: true },
    { title: '菜单名称', field: 'name' },
    {
      title: '图标', field: 'icon', formatter: function (row: any): any {
        return '<i class="' + row.icon + '">';
      }
    },
    { title: 'URL', field: 'url' },
    { title: '排序', field: 'rank' },
  ];

  // 表格
  grid: Grid = new Grid(this.httpService, this.noticeService, this.confirmDialogService, this.router);

  constructor(
    private httpService: HttpService,
    private noticeService: NoticeService,
    private confirmDialogService: ConfirmDialogService,
    private router: Router) { }


  ngOnInit() {
    this.initGrid();
  }

  search() {
    this.grid.reload();
  }

  add() {
    let checked = this.grid.getCheckedRow();
    let id = checked && checked.id ? checked.id : 0;
    this.router.navigate(['/main/menu/edit', 0, id]);
  }

  edit() {
    let checked = this.grid.getCheckedRowAndCheck();
    if (!checked) {
      return;
    }
    this.router.navigate(['/main/menu/edit', 1, checked.id]);
  }

  remove() {
    this.grid.remove('menu/delete/');
  }

  private initGrid() {
    let options = {
      url: 'menu/list',
      columns: this.columns,
      pagination: false,
    };
    this.grid.initOptions(options);
    this.grid.load();
  }

}
